package RabbitMQ.Topic;

import RabbitMQ.Constants.Constants;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

public class producer {
    public static void main(String[] args) throws IOException, TimeoutException {
//       1.建立连接
        ConnectionFactory connectionFactory=new ConnectionFactory();
        connectionFactory.setHost(Constants.HOST);
        connectionFactory.setPort(Constants.PORT);
        connectionFactory.setUsername(Constants.USER_NAME);
        connectionFactory.setPassword(Constants.PASSWORD);
        connectionFactory.setVirtualHost(Constants.VIRTUAL_HOST);
        Connection connection=connectionFactory.newConnection();
//        2.开启信道
        Channel channel=connection.createChannel();
//        设置交换机
        channel.exchangeDeclare(Constants.TOPIC_EXCHANGE, BuiltinExchangeType.TOPIC,true);
//        3.声明队列
        channel.queueDeclare(Constants.TOPIC_QUEUE3,true,false,false,null);
        channel.queueDeclare(Constants.TOPIC_QUEUE4,true,false,false,null);
//    4.绑定交换机和队列
        channel.queueBind(Constants.TOPIC_QUEUE3,Constants.TOPIC_EXCHANGE,"*.k.*");
        channel.queueBind(Constants.TOPIC_QUEUE4,Constants.TOPIC_EXCHANGE,"*.*.b");
        channel.queueBind(Constants.TOPIC_QUEUE4,Constants.TOPIC_EXCHANGE,"c.#");
//      5.发送消息
        String msg1="hello topic,my routingkey is *.a.*";
        channel.basicPublish(Constants.TOPIC_EXCHANGE,"ada.k.sfa",null,msg1.getBytes(StandardCharsets.UTF_8));
        String msg3="hello topic,my routimgkey is c.#";
        channel.basicPublish(Constants.TOPIC_EXCHANGE,"c.sdf",null,msg3.getBytes(StandardCharsets.UTF_8));
        String msg2="hello topic,my routingkey is *.*.b";
        channel.basicPublish(Constants.TOPIC_EXCHANGE,"ass.hh.b",null,msg2.getBytes(StandardCharsets.UTF_8));
//        6.释放资源
        channel.close();
        connection.close();
    }

}
